﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Bluebunnie.Library.Master.ProjectDataSetTableAdapters;
using System.Data;

namespace Bluebunnie.Library.Master
{
    /// <summary>
    /// Interaction logic for PasswordChanger.xaml
    /// </summary>
    public partial class PasswordChanger : Window
    {
        public PasswordChanger()
        {
            InitializeComponent();
        }

        private void change_Click(object sender, RoutedEventArgs e)
        {
            if (password.Password != verify.Password)
            {
                MessageBox.Show("Password baru dan password verifikasi tidak sama.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            ProjectDataSet set = new ProjectDataSet();
            tbl_userTableAdapter adapter = new tbl_userTableAdapter();
            var userTable = new DataTable();
            adapter.Fill(set.tbl_user);

            foreach (DataRow dr in set.tbl_user.Rows)
            {
                if (dr["ID"].Equals(this.Tag))
                {
                    try
                    {
                        dr["Password"] = password.Password;
                        userTable.AcceptChanges();
                        adapter.Update(dr);
                    }
                    catch
                    {
                        MessageBox.Show("Gagal mengubah password.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                    break;
                }
            }
            MessageBox.Show("Password berhasil diubah.", "Sukses", MessageBoxButton.OK, MessageBoxImage.Information);
            Close();
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }        
    }
}
